package main

import (
	"log"

	"domain-system/config"
	"domain-system/database"
	"domain-system/routes"
	"domain-system/services" // 添加这个导入

	"github.com/gin-gonic/gin"
)

func main() {
	// 加载配置
	cfg := config.Load()

	// 输出数据库配置信息
	log.Printf("Database Type: %s", cfg.DBType)
	if cfg.DBType == "sqlite" {
		log.Printf("SQLite Database: %s", cfg.DatabaseURL)
	} else {
		log.Printf("MySQL Database: %s", cfg.DatabaseURL)
	}

	// 初始化数据库
	db, err := database.Init(cfg.DatabaseURL)
	if err != nil {
		log.Fatal("Failed to connect to database:", err)
	}
	log.Printf("Database connected successfully")

	// 初始化OpenIM服务
	openIMService := services.NewOpenIMService(cfg)

	// 设置Gin模式
	if cfg.Environment == "production" {
		gin.SetMode(gin.ReleaseMode)
	}

	// 创建Gin引擎
	r := gin.Default()

	// 设置路由
	routes.SetupRoutes(r, db)
	// 设置认证路由 - 传递三个参数
	routes.SetupAuthRoutes(r, db, openIMService)
	// 设置帖子路由
	routes.SetupPostRoutes(r, db, openIMService)

	// 启动服务器
	log.Printf("Server starting on port %s", cfg.Port)
	if err := r.Run(":" + cfg.Port); err != nil {
		log.Fatal("Failed to start server:", err)
	}
}
